package com.vn.ducktohome.screen.play;

import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.actions.DelayAction;
import com.badlogic.gdx.scenes.scene2d.actions.RepeatAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.vn.ducktohome.gamelogic.GameControl;

public class Lacoste extends Image {

	private boolean bHidden;
	private float fAngle = 0;
	private float fRadius = 0;
	private float fDirection = 0;
	private float fSpeed = 0;
	private float fTimeHidden = 0;
	
	private AlphaAction alpha, alpha2;
	private DelayAction delayAction2;
	
	private TextureRegionDrawable tRD;
	private float coefficientAngle = 57.3f;
	
	//test
	public Image point;

	public Lacoste(String sConfig) {
		super();
		tRD = new TextureRegionDrawable(GameControl.getAtlas().findRegion("play/lacoste"));
		this.setDrawable(tRD);
		this.setWidth(tRD.getMinWidth());
		this.setHeight(tRD.getMinHeight());
		
		bHidden = false;
		 
		String sProperties[] = sConfig.split(",");
		try {
			fAngle = Float.parseFloat(sProperties[0]);
			fRadius = Float.parseFloat(sProperties[1]);
			fDirection = Float.parseFloat(sProperties[2]);
			fSpeed = Float.parseFloat(sProperties[3]);
			fTimeHidden = Float.parseFloat(sProperties[4]);
			initPosition();
			initRotate();
			if (fTimeHidden > 0)
				repeatHidden();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		point = new Image(GameControl.getAtlas().findRegion("play/point"));
		setName("i am lacoste");
	}
	
	public boolean isHidden(){
		return bHidden;
	}

	public void initPosition() {
		if (fDirection == 0) {
			this.setX(Lake.X_CENTER + fRadius * (float) Math.sin(fAngle / coefficientAngle));
			this.setY(Lake.Y_CENTER + fRadius * (float) Math.cos(fAngle / coefficientAngle));
		} else if (fDirection == 1) {
			this.setX(Lake.X_CENTER + fRadius * (float) Math.sin(360 / coefficientAngle - (fAngle / coefficientAngle)));
			this.setY(Lake.Y_CENTER + fRadius * (float) Math.cos(360 / coefficientAngle - (fAngle / coefficientAngle)));
		}
	}

	public void initRotate() {
		if (fDirection == 0)
			this.setRotation(360 + 105 - fAngle);
		else if (fDirection == 1)
			this.setRotation(fAngle - 105);
	}
	
	private void repeatHidden() {
		DelayAction delayAction = new DelayAction();
		delayAction.setDuration(fTimeHidden);
		
		alpha = new AlphaAction();
		alpha.setAlpha(0.2f);
		alpha.setDuration(0.3f);

		delayAction2 = new DelayAction();
		delayAction2.setDuration(fTimeHidden);

		alpha2 = new AlphaAction();
		alpha2.setAlpha(1f);
		alpha2.setDuration(0.3f);

		SequenceAction sequenceAction = new SequenceAction();
		sequenceAction.addAction(delayAction);
		sequenceAction.addAction(alpha);
		sequenceAction.addAction(delayAction2);
		sequenceAction.addAction(alpha2);
		

		RepeatAction repeat = new RepeatAction();
		repeat.setAction(sequenceAction);
		repeat.setCount(100);
		this.addAction(repeat);
	}

	@Override
	public void act(float delta) {
		super.act(delta);

		invalidate();
		fAngle += 0.7 * fSpeed;
		if (fAngle > 360)
			fAngle = 0;
		initPosition();
		initRotate();
		
		if (delayAction2 != null)
			if ((delayAction2.getTime() > 0)&(delayAction2.getTime() < delayAction2.getDuration()))
				bHidden = true;
			else bHidden = false;
		
		point.setX(getX());
		point.setY(getY());
	}
}